﻿// -------------------------------------------------------------------------
//    @FileName         :    NFRoomModule.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-08
//    @Email			:    445267987@qq.com
//    @Module           :    NFRoomModule
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFRoomModule.h"

#include "NFPacketSendUtil.h"
#include "NFPlayer.h"
#include "NFSession.h"
#include "proto_cs_cmd.nanopb.h"

NFRoomModule::NFRoomModule(NFIPluginManager* p):NFLogicDynamicModule(p)
{
}

NFRoomModule::~NFRoomModule()
{
}

int NFRoomModule::Awake()
{
    RegisterClientMessage(NF_ST_LOGIC_SERVER, E_CS_ROOM_QUERY_REQ);
    return 0;
}

int NFRoomModule::OnHandleClientMessage(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_CS_ROOM_QUERY_REQ:
        {
            iRetCode = OnHandleRoomQuery(packet, pSession, req);
            break;
        }
        default: NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFRoomModule::OnHandleServerMessage(uint32_t msgId, NFDataPackage& packet, uint64_t param1, uint64_t param2)
{
    return 0;
}

int NFRoomModule::OnHandleRoomQuery(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& csMsgReq)
{
    CHECK_NULL(0, pSession);

    NFPlayer* pPlayer = pSession->GetPlayer();
    CHECK_NULL(0, pPlayer);

    const proto_ff::Proto_CSRoomQueryReq* req = &csMsgReq.room_query();

    proto_ff::Proto_CSRsp pkg;
    proto_ff::Proto_CSRoomQueryRsp* pRsp = pkg.mutable_room_query();

    if (req && req->has_origin())
    {
        pRsp->set_origin(req->origin());
    }

    NFPacketSendUtil::SendToClient(pSession, E_CS_ROOM_QUERY_RSP, &pkg);
    return 0;
}
